﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;

public partial class AddtoWatchList : System.Web.UI.Page
{

    #region Public Properties

    bool MemberIsValid = false;

    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        txtjobid.Text = Request.QueryString["jobid"].ToString();
        lblerror.Visible = false;
        if (Session["username"] == null)
        {
            pnllogin.Visible = true;
            pnladdtowatchlist.Visible = false;
        }
        else
        {
            pnllogin.Visible = false;
            pnladdtowatchlist.Visible = true;
        }

    }

    #endregion

    #region Add to WatchList Submit Click

    protected void btnAWLSubmit_Click(object sender, ImageClickEventArgs e)
    {
        WatchListCollection wllist = WatchList.GetWatchListByUserID_JobID(Int32.Parse(Session["userid"].ToString()),Request.QueryString["jobid"].ToString());
        if(wllist.Count>0)
        {
            lblerror.Visible = true;
            lblerror.Text = "This job is already added to your watch list.<br/> <a href='javascript:void(0)' onClick='refreshandclose()'>Click here</a> to close this window.";
            divbuttons.Visible = false;
        }
        else
        {
            WatchList wlobj = WatchList.CreateNew();
            wlobj.JobID_RK_Jobs = Request.QueryString["jobid"].ToString();
            wlobj.UserID_RK_RegisteredUserID = Int32.Parse(Session["userid"].ToString());
            wlobj.Comment = txtcomment.Text;
            wlobj.AddedDate = DateTime.Now;
            WatchList.Update(wlobj);
            lblerror.Visible = true;
            lblerror.Text = "Job added to your watch list successfully. <br/> <a href='javascript:void(0)' onClick='refreshandclose()'>Click here</a> to close this window.";
            divbuttons.Visible = false;
        }
    }

    #endregion

    #region Add to WatchList Cancel Click

    protected void btnAWLCancel_Click(object sender, ImageClickEventArgs e)
    {
        String strscript = "<script language=javascript>refreshandclose();</script>";
        if (!Page.IsStartupScriptRegistered("clientScript"))
            Page.RegisterStartupScript("clientScript", strscript);

    }

    #endregion

    #region Login Submit Click

    protected void bttnLoginSubmit_Click(object sender, ImageClickEventArgs e)
    {
        string errMsg = "";
        RegisteredUser.RbsError result = RegisteredUser.LogonUser(txtUserName.Text, txtpassword.Text, out errMsg);
        if (result == RegisteredUser.RbsError.NO_ERROR)
        {
            lblloginerror.Text = "";
            RegisteredUser ruobj = RegisteredUser.GetUserByUserName(txtUserName.Text);
            
            if (ruobj.RegisteredUserType == "Contractor")
            {
                Session["username"] = ruobj.UserName;
                Session["firstandlastname"] = ruobj.FirstName + " " + ruobj.LastName;
                Session["userid"] = ruobj.RegisteredUserID;
                Session["usertype"] = ruobj.RegisteredUserType;
                pnllogin.Visible = false;
                pnladdtowatchlist.Visible = true;
                MemberIsValid = true;
            }
            else
            {
                divloginbuttons.Visible = false;
                lblloginerror.Visible = true;
                lblloginerror.Text = "You are not registered to awail this functionality, it is only implemented for contractors.<br/><a href='javascript:void(0)' onClick='refreshandclose()'>Click here</a> to close this window.";
                
            }
        }
        else
        {
            lblloginerror.Visible = true;
            lblloginerror.Text = errMsg;
        }
    }

    #endregion

    #endregion

}
